package com.vinobase.model.bean;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;

import javax.persistence.Id;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;



@Entity
public class Region implements Comparable{

    @Id
    private Long id;

    @NotNull
	@Size(min=1, max=25)
    private String name;

    private Key<CountryLocal> country;

    @NotNull
    @Transient
    private Long countryId;

    @Transient
    private String countryName;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Key<CountryLocal> getCountry() {
        return country;
    }

    public void setCountry(Key<CountryLocal> country) {
        this.country = country;
    }

    public Long getCountryId() {
        return countryId;
    }

    public void setCountryId(Long countryId) {
        this.countryId = countryId;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Region country = (Region) o;

        if (id != null ? !id.equals(country.id) : country.id != null) return false;
        if (name != null ? !name.equals(country.name) : country.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }

    public int compareTo(Object o) {
        if(!(o instanceof Region)){
            return -9999;
        }
        Region r = (Region)o;
        int cmp = countryName.compareTo(r.getCountryName());
        if(cmp==0)
            cmp = name.compareTo(r.getName());
        return cmp;
    }
}
